package com.cy.pj.sys.service.impl;

import com.cy.pj.common.annotation.RequiredLog;
import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.pojo.PageObject;
import com.cy.pj.common.pojo.SysLog;
import com.cy.pj.common.pojo.SysRole;
import com.cy.pj.common.vo.SysUserDeptVo;
import com.cy.pj.sys.dao.SysLogDao;
import com.cy.pj.sys.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysLogServiceImpl implements SysLogService {
    @Autowired
    private SysLogDao sysLogDao;
    @RequiredLog(operation = "分页查询用户信息")
    @Override
    public PageObject<SysLog> findPageObjects(String username, Integer pageCurrent) {


//1.对参数进行校验
        if(pageCurrent==null||pageCurrent<1)
            throw new IllegalArgumentException("当前页码不正确");
        //2.基于条件查询总记录数
        //2.1) 执行查询
        int rowCount=sysLogDao.getRowCount(username);
        //2.2) 验证查询结果，假如结果为 0 不再执行如下操作
        if(rowCount==0)
            throw new ServiceException("系统没有查到对应记录");
        //3.基于条件查询当前页记录(pageSize 定义为 2)
        //3.1)定义 pageSize
        int pageSize=5;
        //3.2)计算 startIndex
        int startIndex=(pageCurrent-1)*pageSize;
        //3.3)执行当前数据的查询操作
        List<SysLog> records=
                sysLogDao.findPageObjects(username, startIndex, pageSize);
        //4.对分页信息以及当前页记录进行封装
        //4.1)构建 PageObject 对象
        PageObject<SysLog> pageObject=new PageObject<>();
        //4.2)封装数据
        pageObject.setPageCurrent(pageCurrent);
        pageObject.setPageSize(pageSize);
        pageObject.setRowCount(rowCount);
        pageObject.setRecords(records);
        pageObject.setPageCount((rowCount-1)/pageSize+1);
        //5.返回封装结果。
        return pageObject;


    }
    //此注解描述的方法为一个切入点方法,此方法汇之星在一个新的线程中
@Async//这个线程来自于springboot提供的线程池
@Transactional(propagation = Propagation.REQUIRES_NEW)
    @Override
    public void saveObject(SysLog entity) {
      try { Thread.sleep(5000); }catch (Exception e){}
        sysLogDao.insertObject(entity);
    }

}

